package vn.codespot.hms;

import vn.codespot.hms.model.response.FeesRepsonseModel;
import vn.codespot.hms.slidingmenu.SlidingActivityBase;
import vn.codespot.hms.slidingmenu.SlidingActivityHelper;
import vn.codespot.hms.task.CheckingFeesTask;
import vn.codespot.hms.task.ITaskListener;
import vn.codespot.VisaVietNamAndroid.R;
import vn.codespot.hms.utils.NumberPicker;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class CheckFeesActivity extends AbstractActivity implements SlidingActivityBase {
    public static final String PERSON_VALUE = "person_value";

    public static final String VISA_TYPE_VALUE = "visa_type_value";

    private final int PERSON_DIALOG = 3;

    private final int VISA_TYPE_DIALOG = 4;

    private SlidingActivityHelper mHelper;

    private View mCheckFeeHomeLayout;

    private View mCheckFeeResultLayout;

    private ImageButton mBtnBack;

    private Button mCheckFeeHomeCalculateBtn;

    private View mCheckFeeHomeItem;

    private TextView mCheckFeeHomeItemPersonVal;

    private TextView mVisaTypeHomeVal;

    private int mVisaTypeHomeIndex;

    private String[] mVisaTypesHome;

    private String[] mVisaTypeHomeVals;

    private View mCheckFeeResultItem;

    private TextView mCheckFeeResultItemSrvcFee;

    private TextView mCheckFeeResultItemStampFee;

    private TextView mCheckFeeResultItemUrgentlyFee;

    private ImageButton mBtnShowMenu;

    private Button mApplyVisaNow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mHelper = new SlidingActivityHelper(this);
        setMenu(mHelper);
        mHelper.onCreate(savedInstanceState);
        View v_behide = getLayoutInflater().inflate(R.layout.menu_layout, null);
        setBehindContentView(v_behide);
        View v = getLayoutInflater().inflate(R.layout.check_fee_layout, null);
        setContentView(v);
        setBehindOffset(100);
        setSlidingActionBarEnabled(true);
        mBtnShowMenu = (ImageButton) v.findViewById(R.id.btnShowMenu);
        final RadioGroup radGroupMenu = (RadioGroup) v_behide.findViewById(R.id.radGroupMenu);
        final RadioButton radCheckFee = (RadioButton) v_behide.findViewById(R.id.radCheckFees);
        radCheckFee.setChecked(true);
        mBtnShowMenu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                toggle();
            }
        });

        radGroupMenu.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (mHelper.isShowMenu()) {
                    Intent intent = null;
                    toggle();
                    switch (checkedId) {
                    case R.id.radRegister:
                        intent = new Intent(CheckFeesActivity.this, RegisterActivity.class);
                        startActivity(intent);
                        break;
                    case R.id.radCheckFees:
                        break;
                    case R.id.radCheckStatus:
                        intent = new Intent(CheckFeesActivity.this, CheckStatusActivity.class);
                        startActivity(intent);
                        break;
                    case R.id.radContactUs:
                        intent = new Intent(CheckFeesActivity.this, ContactUsActivity.class);
                        startActivity(intent);
                        break;
                    case R.id.radHowToUse:
                        intent = new Intent(CheckFeesActivity.this, HowToUseActivity.class);
                        startActivity(intent);
                        break;
                    case R.id.radSupport:
                        intent = new Intent(CheckFeesActivity.this, SupportActivity.class);
                        startActivity(intent);
                        break;
                    case R.id.radDisclaimer:
                        intent = new Intent(CheckFeesActivity.this, DisclaimerActivity.class);
                        startActivity(intent);
                        break;
                    case R.id.radHistory:
                        intent = new Intent(CheckFeesActivity.this, HistoryActivity.class);
                        startActivity(intent);
                        break;
                    }
                    radGroupMenu.check(R.id.radCheckFees);
                }
            }
        });

        mApplyVisaNow = (Button) findViewById(R.id.applyVisaNowBtn);
        mApplyVisaNow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                radGroupMenu.check(R.id.radRegister);
                Intent intent = new Intent(CheckFeesActivity.this, RegisterActivity.class);
                intent.putExtra(PERSON_VALUE, mCheckFeeHomeItemPersonVal.getText().toString().trim());
                intent.putExtra(VISA_TYPE_VALUE, mVisaTypeHomeVals[mVisaTypeHomeIndex]);
                startActivity(intent);
                CheckFeesActivity.this.finish();
            }
        });

        mVisaTypesHome = this.getResources().getStringArray(R.array.visatypes);
        mVisaTypeHomeVals = this.getResources().getStringArray(R.array.visatype_values);

        mBtnBack = (ImageButton) findViewById(R.id.btn_back);
        mBtnBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View paramView) {
                showHome();
                togglePage(false, mCheckFeeHomeLayout, mCheckFeeResultLayout);
            }
        });

        mCheckFeeHomeCalculateBtn = (Button) findViewById(R.id.btn_checkFeeHome_calculate);
        mCheckFeeHomeCalculateBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                CheckingFeesTask task = getCheckFeesTask();
                task.execute();
            }
        });

        mCheckFeeHomeItem = findViewById(R.id.checkfee_home_item);
        mCheckFeeResultItem = findViewById(R.id.checkfee_result_item);

        mCheckFeeResultItemSrvcFee = (TextView) mCheckFeeResultItem.findViewById(R.id.servicefee_value);
        mCheckFeeResultItemUrgentlyFee = (TextView) mCheckFeeResultItem.findViewById(R.id.urgentlyfee_value);
        mCheckFeeResultItemStampFee = (TextView) mCheckFeeResultItem.findViewById(R.id.stampfee_value);

        View personHome = mCheckFeeHomeItem.findViewById(R.id.person);
        mCheckFeeHomeItemPersonVal = (TextView) personHome.findViewById(R.id.person_value);
        mCheckFeeHomeItemPersonVal.setText("1");
        mCheckFeeHomeItemPersonVal.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckFeesActivity.this.showDialog(PERSON_DIALOG);
            }
        });

        mVisaTypeHomeIndex = 0;
        View visaTypeHome = mCheckFeeHomeItem.findViewById(R.id.visatype);
        mVisaTypeHomeVal = (TextView) visaTypeHome.findViewById(R.id.visatype_value);
        mVisaTypeHomeVal.setText(mVisaTypesHome[mVisaTypeHomeIndex]);
        mVisaTypeHomeVal.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckFeesActivity.this.showDialog(VISA_TYPE_DIALOG);
            }
        });

        mCheckFeeHomeLayout = findViewById(R.id.checkfee_home);
        mCheckFeeResultLayout = findViewById(R.id.checkfee_result);
    }

    private CheckingFeesTask getCheckFeesTask() {
        ITaskListener feesTaskListener = new ITaskListener() {
            @Override
            public void finish(Object... params) {
                if (params == null || params.length != 1 || params[0] == null) {
                    mCheckFeeResultItemSrvcFee.setText("0");
                    mCheckFeeResultItemStampFee.setText("0");
                    mCheckFeeResultItemUrgentlyFee.setText("0");
                    mApplyVisaNow.setVisibility(Button.GONE);
                    return;
                }
                try {
                    FeesRepsonseModel response = (FeesRepsonseModel) params[0];
                    if (response.isSuccess()) {
                        mCheckFeeResultItemSrvcFee.setText(response.getFeeTotal());
                        mCheckFeeResultItemStampFee.setText(response.getStampFee());
                        mCheckFeeResultItemUrgentlyFee.setText(response.getFeeUrgently());
                        mApplyVisaNow.setVisibility(Button.VISIBLE);
                        showResult();
                        togglePage(true, mCheckFeeHomeLayout, mCheckFeeResultLayout);
                    } else {
                        mCheckFeeResultItemSrvcFee.setText("0");
                        mCheckFeeResultItemStampFee.setText("0");
                        mCheckFeeResultItemUrgentlyFee.setText("0");
                        mApplyVisaNow.setVisibility(Button.GONE);
                        Toast.makeText(CheckFeesActivity.this, response.getErrorMessage(), Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception ignore) {
                    mCheckFeeResultItemSrvcFee.setText("0");
                    mCheckFeeResultItemStampFee.setText("0");
                    mCheckFeeResultItemUrgentlyFee.setText("0");
                    mApplyVisaNow.setVisibility(Button.GONE);
                }
            }
        };
        CheckingFeesTask task = new CheckingFeesTask(CheckFeesActivity.this, feesTaskListener, mVisaTypeHomeVals[mVisaTypeHomeIndex],
                Integer.valueOf(mCheckFeeHomeItemPersonVal.getText().toString().trim()), null, null, null, null, null);
        return task;
    }

    private void togglePage(boolean next, View former, View later) {
        TranslateAnimation backward = null;
        TranslateAnimation forward = null;

        if (next) {
            backward = new TranslateAnimation(mCheckFeeHomeLayout.getWidth(), 0.0f, 0.0f, 0.0f);
            forward = new TranslateAnimation(0.0f, -mCheckFeeHomeLayout.getWidth(), 0.0f, 0.0f);
        } else {
            backward = new TranslateAnimation(0.0f, mCheckFeeHomeLayout.getWidth(), 0.0f, 0.0f);
            forward = new TranslateAnimation(-mCheckFeeHomeLayout.getWidth(), 0.0f, 0.0f, 0.0f);
        }

        backward.setDuration(300);
        backward.setInterpolator(new AccelerateInterpolator(1.0f));
        later.startAnimation(backward);

        forward.setDuration(300);
        forward.setInterpolator(new AccelerateInterpolator(1.0f));
        former.startAnimation(forward);
    }

    protected void showResult() {
        // Set visibilities for view
        mCheckFeeResultLayout.setVisibility(LinearLayout.VISIBLE);
        mCheckFeeHomeLayout.setVisibility(LinearLayout.GONE);

        mBtnBack.setVisibility(View.VISIBLE);
        mBtnShowMenu.setVisibility(View.GONE);
    }

    private void showHome() {

        // Set visibilities for view
        mCheckFeeHomeLayout.setVisibility(LinearLayout.VISIBLE);
        mCheckFeeResultLayout.setVisibility(LinearLayout.GONE);

        mBtnBack.setVisibility(View.GONE);
        mBtnShowMenu.setVisibility(View.VISIBLE);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        AlertDialog.Builder builder = null;
        switch (id) {
        case PERSON_DIALOG:
            builder = new AlertDialog.Builder(CheckFeesActivity.this);
            builder.setTitle(R.string.lbPerson);
            final NumberPicker numberPicker = new NumberPicker(CheckFeesActivity.this, null, Integer.valueOf(mCheckFeeHomeItemPersonVal
                    .getText().toString()));
            numberPicker.setGravity(Gravity.CENTER);
            builder.setView(numberPicker);
            builder.setPositiveButton(getString(R.string.titleOK), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mCheckFeeHomeItemPersonVal.setText(String.valueOf(numberPicker.value));
                }
            });
            return builder.create();
        case VISA_TYPE_DIALOG:
            builder = new AlertDialog.Builder(CheckFeesActivity.this);
            builder.setTitle(R.string.lbVisaType);
            builder.setSingleChoiceItems(mVisaTypesHome, mVisaTypeHomeIndex, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mVisaTypeHomeIndex = which;
                    mVisaTypeHomeVal.setText(mVisaTypesHome[mVisaTypeHomeIndex]);
                    dialog.cancel();
                }
            });
            return builder.create();
        default:
            return null;
        }
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        return;
    }
}
